home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / delftips / ti2811.asc < prev    next >
Encoding:
Text File  |  1996-09-15  |  10.3 KB  |  366 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2811
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  August 17, 1995                          PAGE  :  1/6
  11.  
  12.     TITLE  :  Bitmaps And InterBase BLOB Fields
  13.  
  14.  
  15.  
  16.  
  17. dBASE and Paradox tables provide a BLOB field to store binary data that,
  18. when the stored data is of bitmap-format, work as-is with the TDBImage
  19. component to display images. In Database Desktop, these field types are
  20. listed as Binary and Graphic (for dBASE and Paradox tables, respectively).
  21. However, the process of storing bitmap images in InterBase BLOB fields
  22. and using the stored data with TDBImage components is not as straight-
  23. forward.
  24.  
  25. InterBase tables do not have just one type of BLOB field. There are three
  26. variants, or sub-types: type 0, type 1, and user-defined sub-types. Types
  27. 0 and 1 are pre-defined types. Type 0 BLOB fields (the default type) are
  28. for storing general binary data. Type 1 BLOB fields are for storing text
  29. BLOB data. Either the pre-defined type 0 or a user-defined BLOB sub-type
  30. will allow the automated retrieval of bitmap data from the BLOB field that
  31. is to be displayed in a TDBImage component. Type 0 BLOB fields may be used
  32. for storing bitmap-format data or raw binary data. Here is an example of
  33. manually extracting bitmap data stored in a type 0 BLOB field (Table1-
  34. BLOBField) and displaying the data in a (non-data-aware) TImage component:
  35.  
  36.   procedure TForm1.ExtractBtnClick(Sender: TObject);
  37.   begin
  38.     Image1.Picture.Bitmap.Assign(Table1BLOBField);
  39.   end;
  40.  
  41. This manual method may be used or, more commonly, a data-aware control
  42. would be used so that the display of a given record's bitmap (in a BLOB
  43. field) will be automatically displayed. The TDBImage serves this purpose,
  44. and by setting the DataSource property to the TDataSource component
  45. associated with the underlying table and setting the DataField to the BLOB
  46. field containing the bitmap, the TDBImage component will display the image
  47. stored in the BLOB field and automatically load each record's BLOB field
  48. contents as the record pointer is changed.
  49.  
  50. The Database Desktop utility will allow the creation only of type 0 binary
  51. BLOB fields, no provision was made for user-defined BLOB field sub-types.
  52. If it is desired that a user-defined BLOB sub-type be used to store the
  53. bitmap data, it would need to be created with an SQL statement. Typically
  54. this would be through the WISQL utility, but an appropriate SQL statement
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2811
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  August 17, 1995                          PAGE  :  2/6
  72.  
  73.     TITLE  :  Bitmaps And InterBase BLOB Fields
  74.  
  75.  
  76.  
  77.  
  78. in a TQuery would suffice. Here is an SQL statement that creates a table
  79. with a user-defined BLOB field sub-type:
  80.  
  81.   CREATE TABLE WITHBMP
  82.   (
  83.   FILENAME CHAR(12),
  84.   BITMAP   BLOB SUB_TYPE -1
  85.   )
  86.  
  87. Once a table with a compatible BLOB field is created, storing bitmap data
  88. to the BLOB field and displaying the bitmap images in a TDBImage component
  89. uses the same methods as would be used with dBASE or Paradox tables.
  90.  
  91. There are a number of ways to load a bitmap image into a BLOB field. Three
  92. of the easier methods involve 1) copying the data from the Windows clip-
  93. board into a TDBImage component connected to the BLOB field, 2) using the
  94. LoadFromFile method of the TBLOBField component, and 3) using the Assign
  95. method to copy an object of type TBitmap into the Picture property of a 
  96. TBDBImage.
  97.  
  98. The first method, copying the bitmap from the clipboard, is probably most
  99. handy when an application needs to add bitmaps to a table when the end-
  100. user is running the application. A TDBImage component is used to act as an
  101. interface between the BLOB field in the table and the image stored in the
  102. clipboard. The PasteFromClipboard method of the TDBImage component is
  103. invoked to copy the bitmap data from the clipboard into the TDBImage. When
  104. the record is posted, the image is stored into the BLOB field in the
  105. table.
  106.  
  107. Because the Windows clipboard can contain data in formats other than just
  108. bitmap, it is advisable to check the format prior to calling the CopyFrom-
  109. Clipboard method. To do this, a TClipboard object is created and its Has-
  110. Format method is used to determine if the data in the clipboard is indeed
  111. of bitmap format. Note that to use a TClipboard object, the Clipbrd unit
  112. must be included in the Uses section of the unit that will be creating
  113. the object.
  114.  
  115. Here is an example showing the contents of the clipboard being copied into
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2811
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  August 17, 1995                          PAGE  :  3/6
  133.  
  134.     TITLE  :  Bitmaps And InterBase BLOB Fields
  135.  
  136.  
  137.  
  138.  
  139. a TDBImage component, if the contents of the clipboard are of bitmap
  140. format:
  141.  
  142.   procedure TForm1.Button1Click(Sender: TObject);
  143.   var
  144.     C: TClipboard;
  145.   begin
  146.     C := TClipboard.Create;
  147.     try
  148.       if Clipboard.HasFormat(CF_BITMAP) then
  149.         DBImage1.PasteFromClipboard
  150.       else
  151.         ShowMessage('Clipboard does not contain a bitmap!');
  152.     finally
  153.       C.Free;
  154.     end;
  155.   end;
  156.  
  157. The second method of filling a BLOB field with a bitmap involves loading
  158. the bitmap directly from a file on disk into the BLOB field. This method
  159. lends itself equally well to uses at run-time for the end-user as for
  160. the developer building an application's data. This method uses the Load-
  161. FromFile method of the TBLOBField component, the Delphi representation of
  162. an InterBase BLOB field.
  163.  
  164. The LoadFromFile method of the TBLOBField component requires a single
  165. parameter: the name of the bitmap file to load, which is of type String.
  166. The value for this parameter may come from a number of sources from the
  167. end-user manually keying in a valid file name to the program providing a
  168. string to the contents of the FileName property of the TOpenDialog comp-
  169. onent.
  170.  
  171. Here is an example showing the use of the LoadFromFile method for a TBLOB-
  172. Field component named Table1Bitmap (a field called Bitmap in the table
  173. associated with a TTable component named Table1):
  174.  
  175.   procedure TForm1.Button2Click(Sender: TObject);
  176.   begin
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.   PRODUCT  :  Delphi                                 NUMBER  :  2811
  191.   VERSION  :  All
  192.        OS  :  Windows
  193.      DATE  :  August 17, 1995                          PAGE  :  4/6
  194.  
  195.     TITLE  :  Bitmaps And InterBase BLOB Fields
  196.  
  197.  
  198.  
  199.  
  200.     Table1Bitmap.LoadFromFile(
  201.       'c:\delphi\images\splash\16color\construc.bmp');
  202.   end;
  203.  
  204. The third method uses the Assign method to copy the contents of an object
  205. of type TBitmap into the Picture property of a TDBImage component. An
  206. object of type TBitmap might be the Bitmap property of the Picture object
  207. property of a TImage component or it may be a stand-alone TBitmap object.
  208. As with the method copying the data from the clipboard into a TDBImage
  209. component, the bitmap data in the TDBImage component is saved into the
  210. BLOB field in the table when the record is successfully posted.
  211.  
  212. Here is an example using the Assign method. In this case, a stand-alone
  213. TBitmap object is used as the source of the bitmap data. To put a bitmap
  214. image into the TBitmap, the LoadFromFile method of the TBitmap component
  215. is called.
  216.  
  217.   procedure TForm1.Button3Click(Sender: TObject);
  218.   var
  219.     B: TBitmap;
  220.   begin
  221.     B := TBitmap.Create;
  222.     try
  223.       B.LoadFromFile('c:\delphi\images\splash\16color\athena.bmp');
  224.       DBImage1.Picture.Assign(B);
  225.     finally
  226.       B.Free;
  227.     end;
  228.   end;
  229.  
  230. Going the opposite direction -- extracting a bitmap from an InterBase BLOB
  231. field (without first saving the bitmap out to a file) is a simple process
  232. of using the Assign method of the TBLOBField object to store the contents
  233. of the BLOB field to an object of type TBitmap. A stand-alone TBitmap
  234. object or the Bitmap property of the Picture object property of a TIMage
  235. component are examples of compatible destinations for this operation.
  236.  
  237. Here is an example demonstrating using the Assign method to copy a bitmap
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.   PRODUCT  :  Delphi                                 NUMBER  :  2811
  252.   VERSION  :  All
  253.        OS  :  Windows
  254.      DATE  :  August 17, 1995                          PAGE  :  5/6
  255.  
  256.     TITLE  :  Bitmaps And InterBase BLOB Fields
  257.  
  258.  
  259.  
  260.  
  261. from a BLOB field into a TImage component (Table1Bitmap is the TBLOBfield
  262. for the BLOB field in the table).
  263.  
  264.   procedure TForm1.Button1Click(Sender: TObject);
  265.   begin
  266.     Image1.Picture.Bitmap.Assign(Table1Bitmap);
  267.   end;
  268.  
  269. In this example, the TBLOBField object Table1Bitmap is a BLOB field in an
  270. InterBase table. This TBLOBField object was created using the Fields
  271. Editor. If the Fields Editor is not used to create TFields for the fields
  272. in the table, the fields must be referenced using either the FieldByName
  273. method or the Fields property, both part of the TTable and TQuery comp-
  274. onents. In cases where one of those means is used to reference the BLOB
  275. field in a table, the field reference must be type-cast as a TBLOBField
  276. object prior to using the Assign method. For example:
  277.  
  278.   procedure TForm1.Button1Click(Sender: TObject);
  279.   begin
  280.     Image1.Picture.Bitmap.Assign(TBLOBField(Table1.Fields[1]));
  281.   end;
  282.  
  283. A bitmap stored in a BLOB field may also be copied directly to a stand-
  284. alone TBitmap object. Here is an example showing the creation of a
  285. TBitmap object and storing into it a bitmap from a BLOB field.
  286.  
  287.   procedure TForm1.Button2Click(Sender: TObject);
  288.   var
  289.     B: TBitmap;
  290.   begin
  291.     B := TBitmap.Create;
  292.     try
  293.       B.Assign(Table1Bitmap);
  294.       Image1.Picture.Bitmap.Assign(B);
  295.     finally
  296.       B.Free;
  297.     end;
  298.   en
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.   PRODUCT  :  Delphi                                 NUMBER  :  2811
  313.   VERSION  :  All
  314.        OS  :  Windows
  315.      DATE  :  August 17, 1995                          PAGE  :  6/6
  316.  
  317.     TITLE  :  Bitmaps And InterBase BLOB Fields
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362. DISCLAIMER: You have the right to use this technical information
  363. subject to the terms of the No-Nonsense License Statement that
  364. you received with the Borland product to which this information
  365. pertains.
  366.